import http from "@/http/index.ts"
import {Quotation, QuotationListParam, QuotationShare, QuotationUpdateStatus, ExportRequest, ExportFieldInfo} from '@/api/quotation/type.ts'


// 新增报价单
export const addApi = (data: Quotation) => {
    return http.post("/quotation/createQuotation", data)
}
export const getBOMList = () => {
    return http.post("/quotation/getBOMList")
}

// 编辑
export const editApi = (data: Quotation) => {
    return http.put("/quotation/updateQuotation", data)
}


// 分页条件查询列表
export const getListApi = (data:QuotationListParam) => {
    return http.get("/quotation/getQuotationList",data)
}

// 根据产品的id查询详情信息
export const getDetailByIdApi = (id:number) => {
    return http.get(`/quotation/getQuotationDetail/${id}`,)
}

// 修改报价单状态
export const updateStatusApi = (quotationUpdateStatus:QuotationUpdateStatus) => {
    return http.put(`/quotation/updateQuotationStatus`,quotationUpdateStatus)
}

// 删除报价单
export const deleteApi = (id: number) => {
    return http.delete(`/quotation/deleteQuotation/${id}`)
}

// 批量删除报价单
export const batchDeleteApi = (ids: number[]) => {
    return http.post(`/quotation/batchDeleteQuotation`, ids)
}

// 批量更新报价单状态
export const batchUpdateStatusApi = (params: { ids: number[], activate: number }) => {
    return http.put(`/quotation/batchUpdateStatus`, params)
}

// 批量复制报价单
export const batchCopyApi = (params: { sourceIds: number[], targetProjectId: number }) => {
    return http.post(`/quotation/batchCopy`, params)
}

// 分摊金额
export const updateQuotationShareApi = (quotationShare:QuotationShare) => {
    return http.put(`/quotation/updateQuotationShare`,quotationShare)
}

// 打印报价单
export const printQuotationApi = (ids: number[]) => {
    return http.get(`/quotation/printQuotation`, {  ids: ids.join(',') })
}

// 导入结构件核价
export const importQuotationApi = (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return http.post('/quotation/importQuotation', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 导出结构件核价
export const exportQuotationApi = (params: QuotationListParam) => {
    return http.get('/quotation/exportQuotation', params, {
        responseType: 'blob'
    }).then(response => {
        // 创建Blob链接并触发下载
        const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = '结构件核价数据.xlsx'
        link.click()
        window.URL.revokeObjectURL(link.href)
        return Promise.resolve()
    })
}

// 自定义导出结构件核价
export const exportCustomQuotationApi = (exportRequest: ExportRequest) => {
    return http.post('/quotation/exportCustom', exportRequest, {
        responseType: 'blob'
    }).then(response => {
        // 创建Blob链接并触发下载
        const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = '结构件核价自定义导出.xlsx'
        link.click()
        window.URL.revokeObjectURL(link.href)
        return Promise.resolve()
    })
}

// 获取可导出字段列表
export const getExportFieldsApi = () => {
    return http.get<{code: number, data: ExportFieldInfo[]}>('/quotation/getExportFields')
}

// 下载结构件核价模板
export const downloadQuotationTemplateApi = () => {
    return http.get('/quotation/downloadTemplate', {}, {
        responseType: 'blob'
    }).then(response => {
        // 创建Blob链接并触发下载
        const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = '结构件核价导入模板.xlsx'
        link.click()
        window.URL.revokeObjectURL(link.href)
        return Promise.resolve()
    })
}
